package com.liyujie.dyeingmachine.ui.home.operate;

import android.content.Intent;
import android.os.Bundle;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.liyujie.dyeingmachine.R;
import com.liyujie.dyeingmachine.databinding.ActivityReagentBinding;
import com.liyujie.dyeingmachine.listener.OnGeneralCallback;
import com.liyujie.dyeingmachine.listener.UniversalCallback;
import com.liyujie.dyeingmachine.mobile.dao.ReagentEntityDao;
import com.liyujie.dyeingmachine.mobile.entity.ReagentEntity;
import com.liyujie.dyeingmachine.mobile.event.MessageEvent;
import com.liyujie.dyeingmachine.pop.EditDeletePopup;
import com.liyujie.dyeingmachine.ui.BaseActivity;
import com.liyujie.dyeingmachine.ui.home.operate.adapter.ReagentAdapter;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.enums.PopupAnimation;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 试剂管理
 */
public class ReagentActivity extends BaseActivity<ActivityReagentBinding> implements UniversalCallback {


    /**
     * VM
     */
    private ReagentViewModel reagentViewModel;

    /**
     * 适配器
     */
    private ReagentAdapter mReagentAdapter;

    /**
     * 修改删除
     */
    private EditDeletePopup mEditDeletePopup;
    /**
     * 试剂
     */
    private ReagentEntity mReagentEntity;


    /**
     * 是否绑定
     */
    private boolean bound;


    @Override
    public void initParam() {
        super.initParam();
        bound = getIntent().getBooleanExtra("bound", false);
    }


    @Override
    public void initCreate() {
        super.initCreate();
        //注册，重复注册会导致崩溃
        EventBus.getDefault().register(this);
    }


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_reagent;

    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(MessageEvent mMessageEvent) {
        switch (mMessageEvent.getType()) {
            /**
             * 开启成功
             * 更新页面
             */
            case OPEN_SUCCESSFILLY:
                /**
                 * 刷新页面
                 */
                initData();
                break;
        }
    }


    public void initViewModel() {
        //VM
        reagentViewModel = new ViewModelProvider(getViewModelStore(), new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(ReagentViewModel.class);
        binding.setViewModel(reagentViewModel);

    }


    @Override
    public void initView() {
        super.initView();
        binding.rv.setLayoutManager(new GridLayoutManager(mContext, 2));
        mReagentAdapter = new ReagentAdapter();
        mReagentAdapter.setUniversalCallback(this::onUniversalCallback);
        binding.rv.setAdapter(mReagentAdapter);
        /**
         * 修改删除
         */
        mEditDeletePopup = new EditDeletePopup(mContext);
        mEditDeletePopup.setOnGeneralCallback(new OnGeneralCallback() {
            @Override
            public void onGeneralCallback(int GeneralCallback) {
                mEditDeletePopup.dismiss();
                if (GeneralCallback == 0) {//修改
                    if (null != mReagentEntity && null != reagentViewModel) {
                        reagentViewModel.ModifiedReagent(mReagentEntity);
                    }
                } else {
                    if (null != mReagentEntity) {
                        /**
                         * 删除试剂
                         */
                        mReagentEntity.delete();
                    }
                    initData();
                }
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        /**
         * 展示
         */
        mReagentAdapter.setReagentEntityList(ReagentEntityDao.getInstance().getAllList());
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();


    }

    @Override
    public void onUniversalCallback(int position, Object mObject) {
        /**
         *
         */
        mReagentEntity = (ReagentEntity) mObject;
        /**
         *
         */
        if (bound) {
            Intent intent = new Intent();
            intent.putExtra("ReagentId", mReagentEntity.ReagentId);
            setResult(3002, intent);  // 0 是resultCode
            finish();
        } else {
            /**
             *
             */
            new XPopup
                    .Builder(mContext)
                    .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                    .autoOpenSoftInput(false)
                    .asCustom(mEditDeletePopup)
                    .show();
        }

    }


    @Override
    protected void onDestroy() {
        /**
         * 解绑
         */
        EventBus.getDefault().unregister(this);
        super.onDestroy();

    }
}
